<template>
    <view>
        <view class="tabs">
            <van-dropdown-menu :active-color="mainColor">
                <van-dropdown-item :value="storeId" :options="stores" @change="storeDropdown" />
                <van-dropdown-item :value="value1" :options="option1" @change="statusDropdown" />
            </van-dropdown-menu>
        </view>
        <view class="container" style="padding-top: 45px">
            <block v-if="pkgList.length > 0">
                <view class="item" v-for="(item, index) in pkgList" :key="index">
                    <view class="item-top">
                        <view class="info">
                            <text class="title">{{ item.pkgName }}</text>
                            <text class="time">
                                过期时间 {{ item.expireDay == 0 ? '永久有效' : item.expireDay }}
                                <text v-if="item.expireDay !== 0">天</text>
                            </text>
                        </view>
                        <view class="price">
                            <text style="color: #ee0000">
                                ￥
                                <text style="font-size: 40rpx; font-weight: bold">{{ item.price }}</text>
                            </text>
                        </view>
                    </view>

                    <view class="item-bottom">
                        <view class="line1">
                            <view>适用门店：{{ item.storeName }}</view>
                            <view class="can-time">
                                可用时间：
                                <text v-if="item.enableTime && item.enableTime.length == 24">不限制</text>
                                <text v-else>限{{ item.enableTime }}时可用</text>
                            </view>
                            <view class="can-time">
                                可用星期：
                                <text v-if="item.enableWeek && item.enableWeek.length == 26">不限制</text>
                                <text v-else>限{{ item.enableWeek }}可用</text>
                            </view>
                        </view>
                        <view class="line2">
                            <view>
                                包间限制：
                                <text v-if="item.roomType == null || item.roomType == 0">不限制</text>
                                <text v-if="item.roomType == 1">小包</text>
                                <text v-if="item.roomType == 2">中包</text>
                                <text v-if="item.roomType == 3">大包</text>
                                <text v-if="item.roomType == 4">豪包</text>
                                <text v-if="item.roomType == 5">商务包</text>
                            </view>
                            <view>套餐时长：{{ item.hours }}小时</view>
                        </view>
                        <view class="line2">
                            <!-- <text>节假日可用： {{item.enableHoliday==true?'是':'否'}}</text> -->
                            <view>
                                <text v-if="item.enable">套餐状态： 启用</text>
                                <text v-else>套餐状态： 禁用</text>
                            </view>
                            <view>
                                可余额支付：
                                <text v-if="item.balanceBuy">是</text>
                                <text v-else>否</text>
                            </view>
                        </view>
                        <view class="line3">
                            <button class="enableBtn" :data-item="item" @tap="changeStatus">
                                <text v-if="item.enable">禁用</text>
                                <text v-else>启用</text>
                            </button>
                            <button class="editBtn" :data-item="item" @tap="edit">编辑</button>
                        </view>
                    </view>
                </view>
            </block>
            <block v-else>
                <!-- 无订单和未登录 -->
                <view class="nodata">暂无套餐，请新增</view>
            </block>
        </view>
        <!-- 底部按钮 -->
        <button @tap="goToAddPage" :class="'bottom bg-primary ' + (isIpx ? 'fix-iphonex-button' : '')">新增套餐</button>
    </view>
</template>

<script>
// pages/packageManagement/packageManagement.js
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
const app = getApp();
export default {
    data() {
        return {
            userinfo: {},

            //用户信息
            pkgList: [],

            storeId: '',
            storeName: '',
            index: 0,
            stores: [],

            //门店列表
            option1: [
                {
                    text: '全部状态',
                    value: -1
                },
                {
                    text: '禁用',
                    value: 0
                },
                {
                    text: '启用',
                    value: 1
                }
            ],

            value1: -1,
            isLogin: app.globalData.isLogin,
            pageNo: 1,
            pageSize: 10,
            canLoadMore: true,

            //是否还能加载更多
            mainColor: app.globalData.mainColor,

            status: '',
            isIpx: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        console.log('onLoad', options);
        // this.getuserinfo()
        if (options.storeId) {
            this.setData({
                storeId: options.storeId
            });
        }
        this.setData({
            isLogin: app.globalData.isLogin
        });
        this.getXiaLaListAdmin();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {
        this.getPkgList('refresh');
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this;
        that.pageNo = 1;
        this.getPkgList('refresh');
        that.canLoadMore = true;
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this;
        if (that.canLoadMore) {
            that.pageNo++;
            this.getPkgList('');
        } else {
            uni.showToast({
                title: '我是有底线的...'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        getPkgList: function (e) {
            var that = this;
            let message = '';
            if (app.globalData.isLogin) {
                if (e == 'refresh') {
                    //刷新，page变为1
                    message = '正在加载';
                    that.setData({
                        pkgList: [],
                        //列表数组
                        canLoadMore: true,
                        //是否还能加载更多
                        pageNo: 1
                    });
                }
                http.request(
                    '/member/pkg/admin/getAdminPkgPage',
                    '1',
                    'post',
                    {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        enable: that.status,
                        storeId: that.storeId
                    },
                    app.globalData.userDatatoken.accessToken,
                    message,
                    function success(info) {
                        console.info('套餐列表===');
                        if (info.code == 0) {
                            const newList = info.data.list.map((meal) => ({
                                ...meal,
                                enableWeek: that.convertWeekday(meal.enableWeek)
                            }));
                            if (e == 'refresh') {
                                that.setData({
                                    pkgList: newList
                                });
                                if (info.data.list.length === 0) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                            } else {
                                if (info.data != null && info.data.list.length <= info.data.total) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                                let arr = that.pkgList;
                                let arrs = arr.concat(newList);
                                that.setData({
                                    pkgList: arrs
                                });
                            }
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            stores.unshift({
                                text: '全部门店',
                                value: ''
                            });
                            that.setData({
                                stores: stores,
                                storeId: stores[0].value
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //门店下拉菜单发生变化
        storeDropdown(event) {
            //console.log(event)
            this.stores.map((it) => {
                if (it.value == event.detail) {
                    this.setData({
                        storeId: it.value
                    });
                }
            });
            this.getPkgList('refresh');
        },

        //状态下拉菜单发生变化
        statusDropdown(event) {
            let that = this;
            //console.log('选择的值')
            this.option1.map((it) => {
                if (it.value == event.detail) {
                    //console.log(it.value)
                    if (it.value == -1) {
                        that.setData({
                            status: ''
                        });
                    } else {
                        that.setData({
                            status: it.value
                        });
                    }
                    that.getPkgList('refresh');
                }
            });
        },

        goToAddPage() {
            uni.navigateTo({
                url: '../editPages/editPages'
            });
        },

        //定义一个函数来将数字转换为星期名称：
        convertWeekday(numbers) {
            const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
            return numbers.map((num) => weekDays[num % 7]).join(', ');
        },

        changeStatus: function (e) {
            var that = this;
            let item = e.currentTarget.dataset.item;
            http.request(
                '/member/pkg/admin/enable/' + item.pkgId,
                '1',
                'post',
                {
                    pkgId: item.pkgId
                },
                app.globalData.userDatatoken.accessToken,
                '获取中...',
                function success(info) {
                    if (info.code == 0) {
                        uni.showToast({
                            title: '操作成功',
                            icon: 'none'
                        });
                        that.getPkgList('refresh');
                    } else {
                        uni.showToast({
                            title: info.msg,
                            icon: 'none'
                        });
                    }
                },
                function fail(info) {}
            );
        },

        edit: function (e) {
            var that = this;
            let item = e.currentTarget.dataset.item;
            const params = JSON.stringify(item);
            uni.navigateTo({
                url: '../editPages/editPages?item=' + params
            });
        }
    }
};
</script>
<style>
@import './packageManagement.css';
</style>
